package com.kehutong.charge.dao;

import com.kehutong.charge.dto.MeterReadStatistDTO;
import com.kehutong.charge.dto.MeterStatistDTO;
import com.kehutong.charge.entity.ChargeHouseConfig;
import com.kehutong.charge.entity.MeterReading;
import org.coraframework.orm.annotation.Dao;
import org.coraframework.orm.annotation.Mapper;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.Condition;

import java.util.HashMap;
import java.util.List;

/**
 * @author joneRon
 * @Title: kht
 * @Description: 仪表模块的DAO
 * @date 2021/7/30
 */
@Dao
public interface ChargeMeterDao {

    /**
     *  仪表管理统计
     */
    @Mapper(
            genericType = HashMap.class,
            countSQL = "SELECT count(1) FROM( select id FROM t_charge_meter_manage WHERE ${})a",
            sql= "SELECT b.communityNo,b.communityName, sum(if(meterTypeNo = '162683286736010005',1,0)) meterWattNumber, " +
                    "sum(if(meterTypeNo = '162677288357510000',1,0)) meterWaterNumber,sum(if(meterTypeNo = '162683288095810006',1,0)) meterGasNumber " +
                    "FROM t_charge_meter_manage  b WHERE ${}"
    )
    Page<HashMap> findPage(Condition... condition);

    /**
     *  抄表管理统计
     */
    @Mapper(
            genericType = HashMap.class,
            countSQL = "SELECT count(1) FROM (select id from t_charge_meter_read_statist WHERE ${})a",
            sql= "SELECT meterTypeNo,meterTypeName,communityName,communityNo,readYearMonth,COUNT(*) meterNumber," +
                    "sum(IF(readStatus = 1,1,0)) 'meterReadNot',sum(IF(readStatus = 2,1,0)) 'meterReading' ," +
                    "sum(IFNULL(timeUseNumber,0)) monthTotal FROM t_charge_meter_read_statist WHERE ${}"
    )
    Page<HashMap> findStatist(Condition... condition);



    @Mapper(
            genericType = MeterReading.class,
            sql= "SELECT t1.* FROM t_charge_meter_reading" +
                    " t1 LEFT OUTER JOIN t_charge_meter_reading t2 " +
                    "ON (t1.meterNo = t2.meterNo AND t1.deleted = FALSE AND t1.readYearMonth < t2.readYearMonth) " +
                    "WHERE ${} ;"
    )
    List<MeterReading> findLastReading(Condition condition);


    @Mapper(
            genericType = ChargeHouseConfig.class,
            sql= "SELECT * FROM `t_charge_house_config` WHERE ${}"
    )
    List<ChargeHouseConfig> findHouseList(Condition userFind);
}
